<?php

namespace App\Http\Controllers\home;

use App\Aliapi\SendTemplateSMS;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class Login extends Controller
{
    //
    public function index()
    {
        return view('home.register');
    }
    //用户登录
    public function login()
    {
        return view('home.login');
    }
    public function dologin(Request $request)
    {
        $phone = $request->phone;
        $pwd   = $request->pwd;
        if($phone==''){
            exit(json_encode(array('code'=>1,'msg'=>'手机号不能为空')));
        }
        if($pwd==''){
            exit(json_encode(array('code'=>1,'msg'=>'密码不能为空')));
        }
        $res = Auth::guard('member')->attempt(['phone'=>$phone,'password'=>$pwd]);
        if(!$res){
            exit(json_encode(array('code'=>1,'msg'=>'密码或账号错误，请查证后在登录')));
        }
        DB::table('member')->where('phone',$phone)->update(array('lastlogin'=>time()));
        return json_encode(array('code'=>0,'msg'=>'登录成功'));
    }
    public function logout()
    {
        Auth::guard('member')->logout();
        return json_encode(array('code'=>0,'msg'=>'退出登录成功'));
    }

    //用户注册
    public function register(Request $request)
    {
        $data['phone'] = $request->phone;
        $pwd = $request->pwd;
        $verifycode = $request->verifycode;

        if($data['phone']==''){
            exit(json_encode(array('code'=>1,'msg'=>'手机号不能为空')));
        }
        // session中取验证码
        session_start();
        $store_code = $_SESSION['code'];
        if(strtolower($verifycode) != strtolower($store_code)){
            exit(json_encode(array('code'=>1,'msg'=>'验证码错误'.$store_code)));
        }

        $res = DB::table('member')->where('phone',$data['phone'])->first();
        if($res){
            exit(json_encode(array('code'=>1,'msg'=>'该帐号已存在')));
        }
        $data['password'] = password_hash($pwd,PASSWORD_DEFAULT);
        $data['add_time'] = time();
        DB::table('member')->insert($data);
        exit(json_encode(array('code'=>0,'msg'=>'注册成功')));
    }

    //手机号用户注册
    public function phoneReg()
    {
        return view('home.phoneregister');
    }
    //发送验证码
    public function sendCode(Request $request)
    {
        //1、获取前台要发送的手机号
        $phone = $request->phone;

        //2/生成模板中需要的参数 ，验证码和时间
        //生成水机6位验证码
        $code = rand(100000,999999);
        //将随机数转为json格式
        $arr = json_encode(array('code'=>$code));

        $setting = DB::table('setting')->where('names','site_short')->item();
        if($setting){
            $setting['vals'] = json_decode($setting['vals'],true);
        }

        //3、调用阿里云接口
        $templateSMS = new SendTemplateSMS();
        $res = $templateSMS->sendTemplateSMS($setting['vals']['key'],$setting['vals']['secret'],$setting['vals']['signname'],$setting['vals']['code'],$phone,$arr);

        //5、给客户端返回阿里云的响应结果
        if ($res){
            //4、将验证码存入session

            session_start();

            $_SESSION['phone']=$code;
//            session()->put('phone',$code);
//            session()->put($phone,$code);
            return json_encode(array('code'=>0,'msg'=>'短信发送成功'));
        }
        exit(json_encode(array('code'=>1,'msg'=>'短信发送失败')));

    }
    //验证手机号验证码
    public function doPhoneRegister(Request $request)
    {
        $data['phone'] = $request->phone;
        $pwd = $request->pwd;
        $verifycode = $request->verifycode;

        $res = DB::table('member')->where('phone',$data['phone'])->first();
        if($res){
            exit(json_encode(array('code'=>1,'msg'=>'该帐号已存在')));
        }
        //如果验证码不对

        session_start();

        if ($_SESSION['phone'] == ""){
            exit(json_encode(array('code'=>1,'msg'=>'请接收验证码')));
        }
        $store_code = $_SESSION['phone'];
        if($verifycode != $store_code){
            exit(json_encode(array('code'=>1,'msg'=>'验证码错误')));
        }

        $data['password'] = password_hash($pwd,PASSWORD_DEFAULT);
        $data['add_time'] = time();
        DB::table('member')->insert($data);

        exit(json_encode(array('code'=>0,'msg'=>'注册成功')));

    }
    //忘记密码
    public function forget(Request $request)
    {
        return view('home.phoneforget');
    }
    public function doforget(Request $request)
    {
        $phone = $request->phone;
        $pwd = $request->pwd;
        $verifycode = $request->verifycode;
        session_start();
        $store_code = $_SESSION['phone'];
        if($verifycode != $store_code){
            exit(json_encode(array('code'=>1,'msg'=>'验证码错误')));
        }
        $res = DB::table('member')->where('phone',$phone)->item();
        if ($res){
            $data['password'] = password_hash($pwd,PASSWORD_DEFAULT);
            DB::table('member')->where('phone',$phone)->update($data);
        }else{
            exit(json_encode(array('code'=>1,'msg'=>'您输入的手机号不存在')));
        }
        exit(json_encode(array('code'=>0,'msg'=>'密码修改成功')));
    }
    public function phoneyz(Request $request)
    {
        $phone = $request->phone;
        $item = DB::table('member')->where('phone',$phone)->first();

        if ($item == ""){
            return '可以使用';
//            exit(json_encode(array('code'=>0,'msg'=>'可以使用')));
        }
        return '该号码已经存在';
//        exit(json_encode(array('code'=>1,'msg'=>'该号码已经存在')));

    }



}
